/*----------------------------------------------------------------------------
 * Copyright (c) <2016-2018>, <Huawei Technologies Co., Ltd>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *---------------------------------------------------------------------------*/
 /*----------------------------------------------------------------------------
 * Notice of Export Control Law
 * ===============================================
 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
 * applicable export control laws and regulations.
 *---------------------------------------------------------------------------*/

#include "los_base.h"
#include "los_task.h"
#include "los_swtmr.h"
#include "los_hwi.h"
#include "los_queue.h"
#include "los_event.h"
#include "los_typedef.h"
#include "los_api_msgqueue.h"
#include "los_inspect_entry.h"
#include "csg_sdk_hal.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

static UINT32 g_uwQueue;
static UINT32 uwTask1, uwTask2, uwTask3;
static UINT8 Task_finish_flag=0;
#define TIMEOUT   2000

/* task1 send data */
static VOID *send_Entry(UINT32 uwParam)
{
    UINT32 i = 0,uwRet = 0;		
	static CHAR abuf[8][24] = {0};
    UINT32 uwlen;
	
	LOS_TaskDelay(3000);

    //abuf[i][uwlen -2] = '0' + 1;
    sprintf(&abuf[i], "MsgQueue test %d", i);		
    uwlen = sizeof(abuf[i]);

    /* write abuf data to queue */

    //  log("message 1");
    // uwRet = LOS_QueueWrite(g_uwQueue, &abuf[i], uwlen, LOS_WAIT_FOREVER);
    dprintf("4. task1 start send message\r\n");
    uwRet = LOS_QueueWriteCopy(g_uwQueue, abuf[i],  uwlen, LOS_WAIT_FOREVER);
    if(uwRet != LOS_OK)
    {
        dprintf("send message failure,error:%x \r\n",uwRet);
    }
    else
    {
        dprintf("4. task1 send message:%s ok\r\n",(CHAR *)&abuf[i]);
    }
    LOS_TaskDelay(5);

	
    return NULL;
}


/* task2 recv data */
static VOID *recv_Entry(UINT32 uwParam)
{
    CHAR uwReadbuf[24]={0};
	// UINT32 uwReadbuf;
    UINT32 uwRet = LOS_OK;
    UINT32 uwMsgCount = 0;	
    UINT32 Len = 0;
    UINT64 uwTickCount1,uwTickCount2;
    UINT8 i = 0;
    while (i<2)
    {		
        Len=24;
        /* read data from queue to uwReadbuf */
        uwTickCount1 = LOS_TickCountGet();
        // uwRet = LOS_QueueRead(g_uwQueue, &uwReadbuf, 24, TIMEOUT);
        uwRet = LOS_QueueReadCopy(g_uwQueue, uwReadbuf, &Len, TIMEOUT);
        uwTickCount2 = LOS_TickCountGet();
        if (uwRet != LOS_OK)
        {
            dprintf("5. task2 recv message failure,error:%x \r\n",uwRet);
            dprintf("5. LOS_TickCountGet = %d .\r\n", (UINT32)uwTickCount1);
            dprintf("3. LOS_TickCountGet after TIMEOUT 2000ms = %d .\r\n", (UINT32)uwTickCount2);
        }
        else
        {
     //       log("message 2");
            dprintf("5. task2 receive message:%s \r\n", (CHAR *)uwReadbuf);
            uwMsgCount++;
			//memset(uwReadbuf, 0, 24);
        }
        i++;
        LOS_TaskDelay(5);
    }
	if(LOS_OK != LOS_QueueDelete(g_uwQueue))
	{
		 dprintf("6. delete the queue failed! \r\n");
	}
    else 
		 dprintf("6. delete the queue success! \r\n");
    Task_finish_flag++;
    LOS_TaskDelete(uwTask1);
    LOS_TaskDelete(uwTask2);
    return NULL;
}


UINT32 Example_MsgQueue_timeout_recv(VOID)
{
    UINT32 uwRet = 0;
    TSK_INIT_PARAM_S stInitParam1;

    dprintf("\r\n--------4.9 MsgQueue timeout recv test start!--------\r\n");

    LOS_TaskLock();//lock task schedue
    LOS_TaskDelete(uwTask1);
    LOS_TaskDelete(uwTask2);
    LOS_TaskDelete(uwTask3);

    /* create task1 */
    stInitParam1.pfnTaskEntry = send_Entry;
    stInitParam1.usTaskPrio = 1;
    stInitParam1.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    stInitParam1.pcName = "sendQueue1";
	
    
    uwRet = LOS_TaskCreate(&uwTask1, &stInitParam1);
    if (uwRet != LOS_OK)
    {
        dprintf("create task1 failed!,error:%x \r\n",uwRet);
        return uwRet;
    }
    dprintf("\r\n1. Create task1 to send Queue!\r\n");
		
    /*create task2*/	
    stInitParam1.usTaskPrio = 2;
    stInitParam1.pfnTaskEntry = recv_Entry;	
    stInitParam1.pcName = "recvQueue";
    uwRet = LOS_TaskCreate(&uwTask2, &stInitParam1);
    if (uwRet != LOS_OK)
    {
        dprintf("create task2 failed!,error:%x \r\n",uwRet);
        return uwRet;
    }
    dprintf("\r\n2. Create task2 to receive Queue!\r\n");
    /* create queue */
    uwRet = LOS_QueueCreate("queue", 5, &g_uwQueue, 0, 24);
    if (uwRet != LOS_OK)
    {
        dprintf("create queue failure!,error:%x \r\n",uwRet);
    }
    
    dprintf("\r\n3. create 5 queue success!\r\n\n");

    LOS_TaskUnlock();//unlock task schedue
    while(Task_finish_flag<1)
    {
        LOS_TaskDelay(200);
    }
    Task_finish_flag=0;
    return LOS_OK;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
